package com.teach.food.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.exception.CodeException;
import com.teach.food.commons.GetShopsInfo;
import com.teach.food.dto.FoodOrderInfoSearch;
import com.teach.food.entity.FoodOrderInfo;
import com.teach.food.service.FoodMerchantService;
import com.teach.food.service.FoodOrderInfoService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/food/orderInfo")
@Slf4j
public class FoodOrderInfoContorller {
	
	@Autowired
	private FoodOrderInfoService service;
	
	@Autowired 
	FoodMerchantService service2;
	
	@Autowired 
	FoodOrderInfo orderInfo;
	
	@Autowired
	private	FoodOrderInfoSearch search; 
	
	@Autowired
	private GetShopsInfo info;

	@RequestMapping("page-{i}.html")
	public String doPage(Model model,LoginUser user,@PathVariable Integer i,@RequestParam(value = "username",defaultValue = "")String username) {
		search.setFoodOrderInfoUserName(username);
		search.setFoodOrderMerchantName(info.getShopName(user));
		search.setCurrent(i);
		log.debug("{}","666:"+service.pageSearch(search));
		model.addAttribute("p",service.pageSearch(search));
		return "/food/order/orderManage";
	}
	
	@RequestMapping("paidan.do")
	@ResponseBody
	public R doPaidan(@RequestParam(value = "orderInfoId")Integer orderInfoId) {
		orderInfo = service.get(orderInfoId);
		if (orderInfo.getFoodOrderInfoState()==1) {
			throw new CodeException(6010);
		}if (orderInfo.getFoodOrderInfoState()==2) {
			throw new CodeException(6011);
		}
		orderInfo.setFoodOrderInfoState(1);
		log.debug("{}","对象："+orderInfo);
		service.update(orderInfo);
		return R.ok();
	}
	
	@RequestMapping("delete.do")
	@ResponseBody
	public R dodelete(@RequestParam(value = "orderInfoId")Integer orderInfoId) {
		service.delect(orderInfoId);
		return R.ok();
	}
	
	@RequestMapping("update.do")
	@ResponseBody
	public R doupdate(@RequestParam(value = "foodOrderInfoId")Integer foodOrderInfoId,@RequestParam(value = "foodOrderInfoContent")String foodOrderInfoContent) {
		orderInfo = service.get(foodOrderInfoId);
		if (orderInfo.getFoodOrderInfoState()==1) {
			throw new CodeException(6010);
		}if (orderInfo.getFoodOrderInfoState()==2) {
			throw new CodeException(6011);
		}
		orderInfo.setFoodOrderInfoContent(foodOrderInfoContent);
		service.update(orderInfo);
		return R.ok();
	}
}
